package com.feng.controller;

import com.feng.config.PassToken;
import com.feng.dto.LoginUser;
import com.feng.dto.Result;
import com.feng.pojo.User;
import com.feng.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserService userService;

    @PassToken
    @PostMapping("/login")
    public Result login(@RequestBody User user,HttpServletResponse response){
        return userService.login(user,response);
    }

    @PassToken
    @PostMapping("/create")
    public Result create(@RequestBody User user){
        return userService.create(user);
    }

    @PostMapping("/update")
    public Result update(@RequestBody User user, LoginUser loginUser){
        user.setId(loginUser.getId());
        return userService.update(user);
    }

    @PostMapping("/delete")
    public Result delete(@RequestBody User user, LoginUser loginUser, HttpServletRequest request){
        user.setId(loginUser.getId());
        return userService.delete(user,request);
    }
}
